//
//  MyClub.m
//  SelfDrivingTravelling-IOS
//
//  Created by Teplot_01 on 16/8/9.
//  Copyright © 2016年 Teplot_01. All rights reserved.
//

#import "ClubPerson.h"
#import "PersonCell.h"
#import "OutMoneyVC.h"
#import "ClubSettingVC.h"
#import "ClubSystemNews.h"
#import "ClubTipsVC.h"
#import "ClubMessageBoardVC.h"
#import "ClubLeaderManagerVC.h"
#import "ClubMemberManVC.h"
#import "ClubBillVC.h"
#import "ClubPersonInfoVC.h"
@interface ClubPerson ()<UITableViewDelegate,UITableViewDataSource>

@end

@implementation ClubPerson
{
    UITableView *_tableView;
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self requestGetUserWithId];
    [_tableView reloadData];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = LighterGrayColor;
    self.title = @"我的俱乐部";
    self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:nil action:nil];
    
    [self initTableView];
}
//获取用户详情
- (void)requestGetUserWithId{
    
    NSDictionary *parameters = @{
                                 @"id":[NSString readToNSDefaultsWithKey:kId],
                                 };
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.requestSerializer = [AFHTTPRequestSerializer serializer];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    [manager POST:@"http://115.28.152.130/tour/v1/user/getUser.action" parameters:parameters success:^(NSURLSessionDataTask *operation, id responseObject) {
        
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:nil];
        NSLog(@"%@<><>",dic);
        NSNumber *clubUserId = dic[@"object"][@"user"][@"id"];
        NSString *clubUserIdStr = clubUserId.stringValue;
        [clubUserIdStr saveToNSDefaultsWithKey:kClubUserID];
//        [dic[@"object"][@"user"][@"id"] saveToNSDefaultsWithKey:kClubUserID];
        //获取当前时间
        NSDate *currentDate = [NSDate date];//获取当前时间，日期
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateFormat:@"YYYYMMddHHmmssSSS"];
        NSString *dateString = [dateFormatter stringFromDate:currentDate];
        NSString *urlStr = dic[@"object"][@"user"][@"photo"];
        urlStr = [NSString stringWithFormat:@"%@%@%@",urlStr, @"?",dateString];
        [urlStr saveToNSDefaultsWithKey:kClubPhotoUrl];
        //身份证正反面
        NSString *cardPhotoFrontUrl = dic[@"object"][@"user"][@"cardPhotoFront"];
        cardPhotoFrontUrl = [NSString stringWithFormat:@"%@%@%@",cardPhotoFrontUrl, @"?",dateString];
        [cardPhotoFrontUrl saveToNSDefaultsWithKey:kIdCardFimgUrl];
        NSString *cardPhotoBackUrl = dic[@"object"][@"user"][@"cardPhotoBack"];
        cardPhotoBackUrl = [NSString stringWithFormat:@"%@%@%@",cardPhotoBackUrl, @"?",dateString];
        [cardPhotoBackUrl saveToNSDefaultsWithKey:kIdCardBimgUrl];
        
        [dic[@"object"][@"club"][@"clubName"] saveToNSDefaultsWithKey:kClubName];
        [dic[@"object"][@"club"][@"clubSummary"] saveToNSDefaultsWithKey:kClubSummary];
        //club status
        [dic[@"object"][@"club"][@"status"] saveToNSDefaultsWithKey:kStatus];
    } failure:^(NSURLSessionDataTask *operation, NSError *error) {
    }];
}
- (void)initTableView{
    _tableView = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStyleGrouped];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.backgroundColor = LighterGrayColor;
    [self.view addSubview:_tableView];
}
#pragma - mark UITabelViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 4;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0 || section == 2) {
        return 2;
    }
    if (section == 1) {
        return 3;
    }else{
        return 1;
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [[UITableViewCell alloc]init];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    if (indexPath.section == 0 && indexPath.row == 0) {
        UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(10, 10, 70, 70)];
        imageView.layer.masksToBounds = YES;
        imageView.contentMode = UIViewContentModeScaleAspectFill;
        imageView.layer.cornerRadius = 8;
        [imageView sd_setImageWithURL:[NSURL URLWithString:[NSString readToNSDefaultsWithKey:kClubPhotoUrl]] placeholderImage:[UIImage imageNamed:@"userIcon"]];
        [cell.contentView addSubview:imageView];
        UILabel *nameLb = [[UILabel alloc]initWithFrame:CGRectMake(90, 10, 200, 20)];
        NSString *clubStr = [NSString readToNSDefaultsWithKey:kClubName];
        if ([clubStr isEqualToString:@""]) {
            nameLb.text = @"请完善俱乐部名称";
        }else{
            nameLb.text = clubStr;
        }
        nameLb.textColor = Theme_Color;
        [cell.contentView addSubview:nameLb];
        UILabel *accountLb = [[UILabel alloc]initWithFrame:CGRectMake(90, 35, 150, 20)];
        NSString *phoneStr = [NSString readToNSDefaultsWithKey:kPhone];
        accountLb.text = [NSString stringWithFormat:@"%@%@",@"账户名:",phoneStr];
        accountLb.font = [UIFont systemFontOfSize:12];
        [cell.contentView addSubview:accountLb];
        UILabel *emailLb = [[UILabel alloc]initWithFrame:CGRectMake(90, 55, 200, 20)];
        NSString *emailStr = [NSString readToNSDefaultsWithKey:kEmail];
        if ([emailStr isEqualToString:@""]) {
            emailLb.text = @"请完善俱乐部邮箱";
            emailLb.textColor = Theme_Color;
        }else{
            emailLb.text = [NSString stringWithFormat:@"%@%@",@"邮箱:",emailStr];
        }
        emailLb.font = [UIFont systemFontOfSize:12];
        [cell.contentView addSubview:emailLb];
        return cell;
    }
    if (indexPath.section == 0 && indexPath.row == 1) {
        cell.accessoryType = UITableViewCellAccessoryNone;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.backgroundColor = superBackgroundColor;
        UILabel *clubWallet = [[UILabel alloc]initWithFrame:CGRectMake(10, 5, 100, 25)];
        clubWallet.text = @"俱乐部钱包";
        clubWallet.font = [UIFont systemFontOfSize:15];
        [cell.contentView addSubview:clubWallet];
        UILabel *walletLb = [[UILabel alloc]initWithFrame:CGRectMake(10, 30, 100, 25)];
        walletLb.text = @"¥333.00元";
        walletLb.font = [UIFont systemFontOfSize:13];
        [cell.contentView addSubview:walletLb];
        UIButton *intoBtn = [[UIButton alloc]initWithFrame:CGRectMake(KScreenWidth - 130, 25, 50, 25)];
        intoBtn.backgroundColor = White_Color;
        intoBtn.layer.borderWidth = 1;
        intoBtn.layer.borderColor = Gray_Color.CGColor;
        intoBtn.layer.cornerRadius = 3;
        [intoBtn setTitle:@"充值" forState:UIControlStateNormal];
        [intoBtn.titleLabel setFont:[UIFont systemFontOfSize:13]];
        [intoBtn setTitleColor:Gray_Color forState:UIControlStateNormal];
        [cell.contentView addSubview:intoBtn];
        UIButton *outBtn = [[UIButton alloc]initWithFrame:CGRectMake(KScreenWidth - 70, 25, 50, 25)];
        outBtn.backgroundColor = White_Color;
        outBtn.layer.borderWidth = 1;
        outBtn.layer.borderColor = Gray_Color.CGColor;
        outBtn.layer.cornerRadius = 3;
        [outBtn setTitle:@"提现" forState:UIControlStateNormal];
        [outBtn.titleLabel setFont:[UIFont systemFontOfSize:13]];
        [outBtn setTitleColor:Gray_Color forState:UIControlStateNormal];
        [cell.contentView addSubview:outBtn];
        return cell;
    }
    cell.textLabel.font = [UIFont systemFontOfSize:15];
    NSArray *titleArr = [[NSArray alloc] initWithObjects:@"俱乐部账单",@"领队管理",@"会员管理",@"留言板",@"旅游Tips",@"设置",nil];
    NSArray *iconArr = [[NSArray alloc] initWithObjects:@"julebuzhangdan",@"lingdui",@"huiyuanguanli",@"liuyanban",@"lvyoutips",@"setting",nil];
    if (indexPath.section == 1) {
        cell.textLabel.text = [titleArr objectAtIndex:indexPath.row];
        cell.imageView.image = [UIImage imageNamed:[iconArr objectAtIndex:indexPath.row]];
    }
    if (indexPath.section == 2) {
        cell.textLabel.text = [titleArr objectAtIndex:indexPath.row+3];
        cell.imageView.image = [UIImage imageNamed:[iconArr objectAtIndex:indexPath.row+3]];
    }
    if (indexPath.section == 3) {
        cell.textLabel.text = [titleArr objectAtIndex:indexPath.row+5];
        cell.imageView.image = [UIImage imageNamed:[iconArr objectAtIndex:indexPath.row+5]];
    }
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSInteger index = [tableView indexWithIndexPath:indexPath];
    switch (index) {
        case 0:
        {
            ClubPersonInfoVC *vc = [[ClubPersonInfoVC alloc]init];
            vc.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;

        case 2:
        {
            ClubBillVC *vc = [[ClubBillVC alloc]init];
            vc.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
        case 3:
        {
            ClubLeaderManagerVC *vc = [[ClubLeaderManagerVC alloc]init];
            vc.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
        case 4:
        {
            ClubMemberManVC *vc = [[ClubMemberManVC alloc]init];
            vc.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
        case 5:
        {
            ClubMessageBoardVC *vc = [[ClubMessageBoardVC alloc]init];
            vc.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
        case 6:
        {
            ClubTipsVC *vc = [[ClubTipsVC alloc]init];
            vc.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
//        case 8:
//        {
//            ClubSystemNews *vc = [[ClubSystemNews alloc]init];
//            vc.hidesBottomBarWhenPushed = YES;
//            [self.navigationController pushViewController:vc animated:YES];
//        }
//            break;
        case 7:
        {
            ClubSettingVC *vc = [[ClubSettingVC alloc]init];
            vc.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
    }
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0 && indexPath.row == 0) {
        return 90;
    }
    if (indexPath.section == 0 && indexPath.row == 1) {
        return 56;
    }else{
        return 40;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section == 0 || section == 1) {
        return 0.01;
    }
    return 10;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.01;
}
//Btn点击事件
- (void)intoMoneyAction{
    OutMoneyVC *vc = [[OutMoneyVC alloc]init];
    vc.isIntoMoney = YES;
    [self.navigationController pushViewController:vc animated:YES];
}
- (void)outMoneyAction{
    OutMoneyVC *vc = [[OutMoneyVC alloc]init];
    [self.navigationController pushViewController:vc animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
